﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using PowerCreator.LMS.Common.Script;
using PowerCreator.LMS.User.Entity;
using PowerCreator.LMS.Common.Utils;
using PowerCreator.LMS.Web.User;

public partial class sysadmin_UserManager_EditorStudent :PowerCreator.LMS.Web.Base.BasePage
{
    User opUser = new User();
    Department opDepartment = new Department();
    LogicServer.NetMeetingClass objNetMeetingClass;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            InitPage();
            BinderDepartmentClass();

            string uid = Request.QueryString["StudentId"].ToString();
            ViewState["StudentID"] = uid;
            BinderPage();
            if (PowerCreator.LMS.Web.Config.WebConfig.IsUseNetMeeting)
            {
                objNetMeetingClass = new LogicServer.NetMeetingClass();
            }
        }
    }

    private void BinderDepartmentClass()
    {
        string search = "DepartmentType=2 ";
        DataTable dt = opDepartment.Query(search);
        ddl_Department.DataSource = dt;
        ddl_Department.DataTextField = "DepartmentName";
        ddl_Department.DataValueField = "DepartmentId";
        ddl_Department.DataBind();
    }
    protected override void InitPage()
    {
        lbl_InfoTitle_EditStudent.Text = GetLanageValueByKey("lbl_EditStudent_InfoTitle", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_Account_loading.Text = GetLanageValueByKey("lbl_EditStudent_Account_loading", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_LoadingName.Text = GetLanageValueByKey("lbl_EditStudent_LoadingName", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_PassWord.Text = GetLanageValueByKey("lbl_EditStudent_PassWord", PowerCreator.LMS.Language.LanagetItemType.Page);
        lblUserLoginIP.Text = GetLanageValueByKey("lbl_AddStudent_IsLoginIP", PowerCreator.LMS.Language.LanagetItemType.Page);

        lbl_BaseInformaition.Text = GetLanageValueByKey("lbl_EditStudent_BaseInformaition", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_EditStudent_Name.Text = GetLanageValueByKey("lbl_EditStudent_Name", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_Number.Text = GetLanageValueByKey("lbl_EditStudent_Number", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_Sex.Text = GetLanageValueByKey("lbl_EditStudent_Sex", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_IdNumber.Text = GetLanageValueByKey("lbl_EditStudent_IdNumber", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_Born.Text = GetLanageValueByKey("lbl_EditStudent_Born", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_Mail.Text = GetLanageValueByKey("lbl_EditStudent_Mail", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_Class.Text = GetLanageValueByKey("lbl_EditStudent_Class", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_Nation.Text = GetLanageValueByKey("lbl_EditStudent_Nation", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_Phone.Text = GetLanageValueByKey("lbl_EditStudent_Phone", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_Native.Text = GetLanageValueByKey("lbl_EditStudent_Native", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_Nation.Text = GetLanageValueByKey("lbl_EditStudent_Nation", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_Post.Text = GetLanageValueByKey("lbl_EditStudent_Post", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_MobileTelephone.Text = GetLanageValueByKey("lbl_EditStudent_MobileTelephone", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_Editress.Text = GetLanageValueByKey("lbl_EditStudent_Editress", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_Remark.Text = GetLanageValueByKey("lbl_EditStudent_Remark", PowerCreator.LMS.Language.LanagetItemType.Page);
       //lab_Birth_css.Text = "(格式：1980-01-01)";
        radbtn_Male.Text = GetLanageValueByKey("EditUser_desc27", PowerCreator.LMS.Language.LanagetItemType.Page);
        radbtn_Female.Text = GetLanageValueByKey("EditUser_desc28", PowerCreator.LMS.Language.LanagetItemType.Page);

        rfv_txtUserName.ErrorMessage = GetLanageValueByKey("AddTeacher_msg_g1", PowerCreator.LMS.Language.LanagetItemType.Page);
        rfv_txtRealName.ErrorMessage = GetLanageValueByKey("AddTeacher_msg_g2", PowerCreator.LMS.Language.LanagetItemType.Page);
        //ftv_txtUserPwd.ErrorMessage = GetLanageValueByKey("AddTeacher_msg_g4", PowerCreator.LMS.Language.LanagetItemType.Page);

        btn_EditNew.Text = GetLanageValueByKey("btn_EditNew", PowerCreator.LMS.Language.LanagetItemType.Page);
        btn_Reset.Text = GetLanageValueByKey("btn_Reset", PowerCreator.LMS.Language.LanagetItemType.Page);

        base.InitPage();
    }

    private void BinderPage()
    {
        VOTStudent vo = opUser.LoadStudent(int.Parse(ViewState["StudentID"].ToString()));
        VOTUser voUser = opUser.LoadUser(vo.FKStudentUserId);
        txt_UserName.Text = voUser.UserName;
        txt_UserName.Enabled = false;
        txt_UserloginIP.Text = voUser.UserLoginIP;
        //txt_UserPwd.Text = voUser.UserPassword;
        //txt_UserPwd.Enabled = false;
        txt_RealName.Text = vo.StudentName;
        txt_StudentCode.Text = vo.StudentCode;
        txt_Address.Text = vo.StudentAddress;
        txt_Birth.Text = vo.StudentBirthday.ToShortDateString();
        txt_Description.Text = vo.StudentDescription;
        txt_Email.Text = vo.StudentEmail;
        txt_Homeplace.Text = vo.StudentHomeplace;
        txt_Idcard.Text = vo.StudentIdentityCard;
        txt_Nation.Text = vo.StudentNation;
        txt_Phone.Text = vo.StudentMobileTelephone;
        txt_Post.Text = vo.StudentPost;
        txt_Tel.Text = vo.StudentTelephone;
        if (vo.StudentSex == 0)
        {
            radbtn_Male.Checked = true;
            radbtn_Female.Checked = false;
        }
        else
        {
            radbtn_Male.Checked = false;
            radbtn_Female.Checked = true;
        }

        if (!string.IsNullOrEmpty(vo.FKStudentDepartmentId.ToString()))
        {
            for (int i = 0; i < ddl_Department.Items.Count; i++)
            {
                if (ddl_Department.Items[i].Value == vo.FKStudentDepartmentId.ToString())
                {
                    ddl_Department.Items[i].Selected = true;
                    break;
                }
            }
        }

    }
    private bool CheckPageData()
    {
        if (!string.IsNullOrEmpty(txt_Birth.Text))
        {
            if (!TimeUtils.IsDateTime(txt_Birth.Text.Trim()))
            {
                lbl_msg_Description.Text = GetLanageValueByKey("EditorTeacher_msg_g1", PowerCreator.LMS.Language.LanagetItemType.Page);
                txt_Birth.BackColor = System.Drawing.Color.BlanchedAlmond;
                txt_Birth.Focus();
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "showmy1", "<script>AutoColseAler()</script>");
                return false;
            }
        }
        if (!string.IsNullOrEmpty(txt_Email.Text))
        {
            if (!PageValidate.IsEmail(txt_Email.Text.Trim()))
            {
                lbl_msg_Description.Text = GetLanageValueByKey("EditorTeacher_msg_g3", PowerCreator.LMS.Language.LanagetItemType.Page);
                txt_Email.BackColor = System.Drawing.Color.BlanchedAlmond;
                txt_Email.Focus();
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "showmy1", "<script>AutoColseAler()</script>");
                return false;
            }
        }
        //if (!string.IsNullOrEmpty(txt_Tel.Text))
        //{
        //    if (!PageValidate.IsPhone(txt_Phone.Text))
        //    {
        //        lbl_msg_Description.Text = GetLanageValueByKey("EditorTeacher_msg_g4", PowerCreator.LMS.Language.LanagetItemType.Page);
        //        txt_Tel.BackColor = System.Drawing.Color.BlanchedAlmond;
        //        txt_Tel.Focus();
        //        Page.ClientScript.RegisterStartupScript(Page.GetType(), "showmy1", "<script>AutoColseAler()</script>");
        //        return false;
        //    }
        //}
        if (!string.IsNullOrEmpty(txt_Phone.Text))
        {
            if (!PageValidate.IsNumber(txt_Phone.Text))
            {
                lbl_msg_Description.Text = GetLanageValueByKey("EditorTeacher_msg_g5", PowerCreator.LMS.Language.LanagetItemType.Page);
                txt_Phone.BackColor = System.Drawing.Color.BlanchedAlmond;
                txt_Phone.Focus();
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "showmy1", "<script>AutoColseAler()</script>");
                return false;
            }
        }

        //if (!string.IsNullOrEmpty(txt_Tel.Text))
        //{
        //    if (!PageValidate.IsNumber(txt_Tel.Text))
        //    {
        //        lbl_msg_Description.Text = GetLanageValueByKey("EditorTeacher_msg_g6", PowerCreator.LMS.Language.LanagetItemType.Page);
        //        txt_Tel.BackColor = System.Drawing.Color.BlanchedAlmond;
        //        txt_Tel.Focus();
        //        Page.ClientScript.RegisterStartupScript(Page.GetType(), "showmy1", "<script>AutoColseAler()</script>");
        //        return false;
        //    }
        //}

        return true;
    }
    protected void btnBack_Click(object sender, EventArgs e)
    {
        string url = "StudentManager.aspx";
        Script.RedirectURL(url);
    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        if (!CheckPageData()) return;
        VOTStudent vo = opUser.LoadStudent(int.Parse(ViewState["StudentID"].ToString()));

        VOTUser voUser = opUser.LoadUser(vo.FKStudentUserId);
        voUser.UserLoginIP = txt_UserloginIP.Text;
        opUser.Update(voUser);

        if(!string.IsNullOrEmpty(ddl_Department.SelectedValue.ToString()))
            vo.FKStudentDepartmentId = int.Parse(ddl_Department.SelectedValue.ToString());
        vo.StudentAddress = txt_Address.Text;
        if (!string.IsNullOrEmpty(txt_Birth.Text))
            vo.StudentBirthday = DateTime.Parse(txt_Birth.Text);
        vo.StudentCode = txt_StudentCode.Text;
        vo.StudentDescription = txt_Description.Text;
        vo.StudentEmail = txt_Email.Text;
        vo.StudentHomeplace = txt_Homeplace.Text;
        vo.StudentIdentityCard = txt_Idcard.Text;
        vo.StudentMobileTelephone = txt_Phone.Text;
        vo.StudentName = txt_RealName.Text;
        vo.StudentNation = txt_Nation.Text;
        vo.StudentPost = txt_Post.Text;
        if (radbtn_Male.Checked)
            vo.StudentSex = 0;
        else
            vo.StudentSex = 1;
        vo.StudentTelephone = txt_Tel.Text;
        
        if (opUser.UpdateStudent(vo))
        {
            if (PowerCreator.LMS.Web.Config.WebConfig.IsUseNetMeeting)
            {
                objNetMeetingClass.WebModifyUser(voUser.UserId, voUser.UserName, voUser.UserPassword, (ulong)voUser.UserRule, voUser.UserImageIndex, vo.StudentDescription, vo.StudentSex, vo.StudentTelephone, voUser.UserType);
            }
            
            string url = "StudentManager.aspx";
            Script.RedirectURL(url);
        }
    }
}
